package cn.wizzer.app.onlyoffice.modules.services.impl;

import cn.wizzer.app.onlyoffice.modules.config.OnlyOfficeProperties;
import cn.wizzer.app.onlyoffice.modules.models.documentServer.Action;
import cn.wizzer.app.onlyoffice.modules.models.documentServer.DefaultCustomizationWrapper;
import cn.wizzer.app.onlyoffice.modules.models.documentServer.DefaultFileWrapper;
import cn.wizzer.app.onlyoffice.modules.models.documentServer.EditorConfig;
import cn.wizzer.app.onlyoffice.modules.services.EditorConfigConfigurer;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import java.util.HashMap;

@IocBean
public class EditorConfigConfigurerImpl implements EditorConfigConfigurer<DefaultFileWrapper> {
    @Inject
    private OnlyOfficeProperties onlyOfficeProperties;
    @Inject
    private CustomizationConfigurerImpl customizationConfigurer;

    @Override
    public void configure(EditorConfig editorConfig, DefaultFileWrapper wrapper) {
        // 不显示新建
        editorConfig.setTemplates(null);
        editorConfig.setCreateUrl(null);

        // 设置回调接口
        editorConfig.setCallbackUrl(onlyOfficeProperties.getCallbackUrl() +wrapper.getBusinessFileInfo().getId());
        editorConfig.getCustomization().setLogo(onlyOfficeProperties.getLogo());
        editorConfig.getCustomization().setCustomer(onlyOfficeProperties.getCustomer());
        editorConfig.setLang("zh"); // 语言，写死不允许修改
        Boolean canEdit = wrapper.getCanEdit();
        Action action = wrapper.getAction();
        editorConfig.setCoEditing(action.equals(Action.view) ? new HashMap<String, Object>() {{
            put("mode", "strict");
            put("change", false);
        }} : new HashMap<String, Object>() {{
            put("mode", "fast");
            put("change", true);
        }});

        //  define the customization configurer
        customizationConfigurer.configure(editorConfig.getCustomization(),
                DefaultCustomizationWrapper.builder()
                        .action(action)
                        .build());

        // 设置Model
        editorConfig.setMode(canEdit && !action.equals(Action.view) ? EditorConfig.Mode.edit : EditorConfig.Mode.view);
        editorConfig.setUser(wrapper.getUser());
        // 设置embedded
        editorConfig.setEmbedded(null);
    }
}
